<template>
  <div>
     <pre>
           //{{underlineToCamel($props.tableName)}}.Service.ts


import { Inject, Controller, Get, Query, Post, Body, App, All,Provide  } from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { HttpService } from "@midwayjs/axios";
import { Application } from "../interface.js";
import { failres, okres } from "../config/resUtil.js";
import { InjectEntityModel } from "@midwayjs/typeorm";
import { Repository } from "typeorm";
import {  {{entityName}}  } from "../entity/{{underlineToCamel($props.tableName)}}.entity.js";
import { BaseDto } from "../models/Base.dto.js";
import { getSkip } from "../utils/pageUtil.js";


          @Provide('/api/{{$props.tableName}}')
        export class  {{upperFirstConcat(underlineToCamel($props.tableName))}}Service {
              @Inject()
              ctx: Context;
              @App("koa")
              app: Application;

              @InjectEntityModel({{entityName}}, "default")
              {{underlineToCamel($props.tableName)}}Model: Repository<{{entityName}}>;

 //find
    async getlist(body: {{entityName}} & BaseDto) {
      var query = this.{{underlineToCamel($props.tableName)}}Model
        .createQueryBuilder("{{$props.tableName}}").select();
          query=query.where('1=1')

        <div v-for="(colitem,index) in ($props.columns||[])" :key="index">

            if (body?.{{underlineToCamel(colitem.colkey)}}) {
            query = query.andWhere(`{{colitem.colkey}} = :{{underlineToCamel(colitem.colkey)}}`,{
              {{underlineToCamel(colitem.colkey)}}:body?.{{underlineToCamel(colitem.colkey)}}
            });
           }
        </div>
      var result3 = await query.skip(getSkip(body)).take(body.pageSize).getManyAndCount();
      var datalist = (result3?.[0] || []) as unknown as {{entityName}}[];
      return okres(datalist, { total: result3?.[1] });
    }
 //findone
    async getbyid( id) {
      var query = this.{{underlineToCamel($props.tableName)}}Model
        .createQueryBuilder("{{$props.tableName}}").select();
            query=query.where('1=1')
            query = query.andWhere(`id = :id`,{id});
      var result3 = await query.getOne();
      return okres(result3);
    }
 //add
    async add(body: {{entityName}} & BaseDto) {
    delete body?.id;
      var result =await  this.{{underlineToCamel($props.tableName)}}Model.save(body)
      return okres(result);
    }
 //update
    async update(@Body() body: {{entityName}} & BaseDto) {
      var result =await  this.{{underlineToCamel($props.tableName)}}Model.update(body?.id,body)
      return okres(result);
    }
 //delbyid
    async delbyid( id:number) {
      var result =await  this.{{underlineToCamel($props.tableName)}}Model.delete(id)
      return okres(result);
    }


        }
      </pre>
  </div>
</template>
<script >


function type2tstype(texttype){
 if(texttype=='integer'){
 return 'number'
 }else if(texttype=='text'){
return 'string'
 }
}
function upperFirstConcat(str) {
   str = str.toLowerCase();
    str = str.replace(/\b\w+\b/g, word => {
      return word.substring(0, 1).toUpperCase() + word.substring(1);
  });

  return str;
}
function underlineToCamel(str) {
  return str?.replace(/_([a-z])/g, function(match, group1) {
    return group1.toUpperCase();
  });
}
export  default {
  data() {
    return {
      upperFirstConcat,
      type2tstype,
      underlineToCamel,
    };
  },

  props:{
    columns:{type:[Array]},
    tableName:{type:[String]}
  },
  computed:{
   entityName(){
   return `${underlineToCamel(this.$props.tableName)}Entity`
   }
  }
}
</script>
